---
sidebar_label: TogetherAI
---

import CodeBlock from "@theme/CodeBlock";

# ChatTogetherAI

## Setup

1. Create a TogetherAI account and get your API key [here](https://api.together.xyz/).
2. Export or set your API key inline. The ChatTogetherAI class defaults to `process.env.TOGETHER_AI_API_KEY`.

```bash
export TOGETHER_AI_API_KEY=your-api-key
```

You can use models provided by TogetherAI as follows:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/community
```

import UnifiedModelParamsTooltip from "@mdx_components/unified_model_params_tooltip.mdx";

<UnifiedModelParamsTooltip></UnifiedModelParamsTooltip>

import TogetherAI from "@examples/models/chat/integration_togetherai.ts";

<CodeBlock language="typescript">{TogetherAI}</CodeBlock>

## Tool calling & JSON mode

The TogetherAI chat supports JSON mode and calling tools.

### Tool calling

import TogetherToolsExample from "@examples/models/chat/integration_togetherai_tools.ts";

<CodeBlock language="typescript">{TogetherToolsExample}</CodeBlock>

:::tip
See a LangSmith trace of the above example [here](https://smith.langchain.com/public/5082ea20-c2de-410f-80e2-dbdfbf4d8adb/r).
:::

### JSON mode

To use JSON mode you must include the string "JSON" inside the prompt.
Typical conventions include telling the model to use JSON, eg: `Respond to the user in JSON format`.

import TogetherJSONModeExample from "@examples/models/chat/integration_togetherai_json.ts";

<CodeBlock language="typescript">{TogetherJSONModeExample}</CodeBlock>

:::tip
See a LangSmith trace of the above example [here](https://smith.langchain.com/public/3864aebb-5096-4b5f-b096-e54ddd1ec3d2/r).
:::

Behind the scenes, TogetherAI uses the OpenAI SDK and OpenAI compatible API, with some caveats:

- Certain properties are not supported by the TogetherAI API, see [here](https://docs.together.ai/reference/chat-completions).
